const mixin = {
  data: {
    // 当前登录医生信息
    minDate:new Date(2025, 2, 1),
    maxDate:new Date(),
    doctorTestData: {
      doctorInfoId: "",
      doctorName: '',
      doctorType: "" // doctorInCharge-主管医生; dutyDoctor-值班医生
    },
    // 当前患者个人信息
    patientData: {
      patientName: '',
    },
    AllPatientList: [],
    // 患者综合信息
    patientInfo: {
      warningId: "",//,string //当前预警ID",
      confirmColor: "",//,string //预警颜色: 红色-收到预警未确认预警; 黄色-收到预警已确认但未反馈 未反馈是指当前预警ID在确认预警的那个时间以及之后没有诊断记录、没有治疗方案",
      patientInfoId: "",//, //病患信息主键ID",
      hospitalInfoId: "", //医院信息主键ID",
      isPatientDischarged: "",//string //病患是否出院 1-是;0-否",
      dischargedTime: "",//病患出院的时间",
      doctorInfoId: "", //int //医生主键【必须】
      doctorName: "",
      patientId: "", //string //患者ID【必须】
      name: "", //string //患者名称【必须】
      gender: "", //string //患者性别【必须】
      age: "", //string //患者年龄【必须】
      admissionTime: "", //date //患者入院时间【必须】
      firstIcuAdmissionTime: "", //date //患者首次ICU入院时间
      degreeOfSepsis: "", //double //患者确诊患脓毒症的程度(只用传参具体占比就行,不用带%号)
      isDiagnosedWithSepsis: "", //是否确诊为脓毒症 1-是;0-否
      diagnosedTime: "", //date //确诊为脓毒症的时间
      isDieWithin28Days: "",
      retrospectiveDiagnosedTime:"",//脓毒回顾时间时间
      dischargedTime:"",
      dieTime:"",
      patientDiseaseDetailList: [
        // {
        //   // diseaseInfoId: "123", //int //病症主键ID
        //   // value: "0", // 病症值
        //   // "patientDiseaseInfoId":"int //病患病症信息主键ID",
        //   // "doctorInfoId":"int //医生信息主键ID",
        //   // "patientInfoId":"int //病患信息主键ID",
        //   // "diseaseInfoId":"int //病症信息主键ID",
        //   // "value":"string //病症值",
        //   // "createdDate":"date",
        //   // "createdBy":"string",
        //   // "updatedDate":"date",
        //   // "updatedBy":"string"
        // }
      ],
      createdBy: "", //string //创建人
      updatedBy: "", //string //更新人
      isContainDiagnosis:"", // 是否包含诊断信息 1包含0 未包含
      isContainTreatment:"" // 是否包含质量治疗方案 1包含0 未包含
    },
  },
  created () {
    // new VConsole()
    // console.log(this.Encryption("0304"))
    // console.log(this.Encryption("测试数据"))
    // const data1 =  this.Encryption("0304")
    // console.log(this.decryption(data1))
    this.maxDate = this.getTodayEnd()
  },
  methods: {
    getTodayEnd() {
      const today = new Date();
      today.setHours(23, 59, 59, 0);
      console.log(today)
      return today;
    },
    getValueByPath(data, path, defaultValue = undefined) {
      // 将路径统一转换为数组
      const keys = Array.isArray(path) ? path : path.split('.');
    
      // 逐级访问对象属性
      let result = data;
      for (const key of keys) {
        if (result && typeof result === 'object' && key in result) {
          result = result[key];
        } else {
          return defaultValue; // 路径不存在时返回默认值
        }
      }
    
      return result;
    },
    setValueByPath(target, path, value, defaultValue = null) {
      // 将路径统一转换为数组
      const keys = Array.isArray(path) ? path : path.split('.');
    
      // 逐级访问目标对象
      let current = target;
      for (let i = 0; i < keys.length - 1; i++) {
        const key = keys[i];
    
        // 如果当前层级不存在，创建空对象或数组
        if (!current[key] || typeof current[key] !== 'object') {
          current[key] = /^\d+$/.test(keys[i + 1]) ? [] : {}; // 如果下一级是数字，创建数组
        }
        current = current[key];
      }
    
      // 对值进行处理
      const lastKey = keys[keys.length - 1];
      try {
        const finalValue = value;
        current[lastKey] = finalValue !== undefined ? finalValue : defaultValue;
      } catch (error) {
        console.error('赋值失败:', error);
        current[lastKey] = defaultValue; // 如果处理失败，使用默认值
      }
    },
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      if (type === 'hour') {
        return val + '时';
      }
      if (type === 'minute') {
        return val + '分';
      }
      return val;
    },
     isNumeric(str) {
      return !isNaN(+str);
    },
    isPositiveInteger(str) {
      return /^[1-9]\d*$/.test(str);
    },
    isBase64 (str) {
      // const regex = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/;
      // return regex.test(str);
      // const base64Pattern = /^[A-Za-z0-9+/=]*$/;
      const base64Pattern = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/;
      return base64Pattern.test(str) && (str.length % 4 === 0);
    },
    // 加密
    base64Encode (str) {
      const utf8Bytes = new TextEncoder().encode(str);  // 将字符串转为 UTF-8 编码字节
      return btoa(String.fromCharCode(...utf8Bytes));  // 使用 btoa 进行 Base64 编码
    },
    // 解码 Base64 编码的字符串
    base64Decode (base64Str) {
      const decodedString = atob(base64Str);  // 使用 atob 解码
      const utf8Bytes = new Uint8Array(decodedString.length);
      for (let i = 0; i < decodedString.length; i++) {
        utf8Bytes[i] = decodedString.charCodeAt(i);
      }
      return new TextDecoder().decode(utf8Bytes);  // 解码成 UTF-8 字符串
    },
    // 加密
    Encryption (e) {
      // 判断当前接受文字是否为base64格式的加密文案 是不再做重复加密，不是则做加密处理 为空则不处理 
      if (e) {
          return this.base64Encode(e);
      } else {
        return e
      }
    },
    // 解密
    decryption (e) {
      if (e) {
        return this.base64Decode(e)
      } else {
        return e
      }
    },
    getUrlParams (url) {
      const params = new URLSearchParams(url.split('?')[1]);
      const paramsObj = {};
      for (const [key, value] of params) {
        paramsObj[key] = value;
      }
      return paramsObj;
    },
    backList () {
      const doctorTestData = sessionStorage.getItem("doctorTestData")
      // 查询当前医生名下患者列表
      if (doctorTestData) {
        // 增加医院id 
        const { doctorInfoId, doctorName, doctorType, doctorId } = JSON.parse(doctorTestData)
        window.location.href = "AI_patient_list.html?doctorInfoId=" + doctorInfoId + "&doctorName=" + doctorName + "&doctorType=" + doctorType + "&doctorId=" + doctorId + "&hospitalInfoId=" + hospitalInfoId;
      }
    },
    setData () {
      if (isLocal) {
        this.doctorTestData = { doctorInfoId: 77, doctorName: "本地联调医生", doctorType: "doctorInCharge" }
        sessionStorage.setItem("doctorTestData", JSON.stringify(this.doctorTestData))
      }
    },
    initData () {
      // 获取医生信息
      const doctorData = sessionStorage.getItem("doctorTestData")
      const patientInfoId = sessionStorage.getItem("patientInfoId")
      if (doctorData) {
        Object.assign(this.patientInfo, JSON.parse(doctorData))
        console.log(this.patientInfo, "当前医生信息")
        // 根据当前医生信息 获取所有患者信息
      }
      this.getPaitentDetail(patientInfoId)
    },
    // 获取患者详情 
    getPaitentDetail (patientInfoId) {
      const URL = "/patient/detail?patientInfoId=" + patientInfoId;
      fetch(apiUrl + URL, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
      })
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          if (data.code === 200) {
            // 更新一下患者id方便切换页面做 病人信息查询
            sessionStorage.setItem("patientInfoId", patientInfoId)
            this.patientInfo.patientDiseaseDetailList = []
            // 对患者id和患者姓名做解密处理
            let endata = {
              patientId: this.Encryption(data.data.patientId),
              name: this.Encryption(data.data.name)
            }
            Object.assign(this.patientInfo, data.data, endata)
            console.log(this.patientInfo, "患者信息")
          } else {
            console.error(data.msg);
          }
        })
        .catch((error) => {
          console.error(error);
        });
    },
    // 获取患者列表
    getAllPatientList () {
      // "doctorInfoId":"string //医生主键id【必须】",
      // "doctorName":"string //医生名称【必须】",
      // "doctorType":"string //医生类别\tdoctorInCharge-主管医生; dutyDoctor-值班医生\t当为值班医生时,不过滤所属医生"
      const doctorData = JSON.parse(sessionStorage.getItem("doctorTestData"))
      console.log(doctorData)
      const URL = "/patient/list/all";
      fetch(apiUrl + URL, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          doctorInfoId: doctorData.doctorInfoId,
          doctorName: doctorData.doctorName,
          doctorType: doctorData.doctorType
        })
      })
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          if (data.code === 200) {
            this.AllPatientList = data.data ?? []
            console.log(data, "所有患者")
          } else {
            console.error(data.msg);
          }
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
}